package com.ruoyi.common.yh.api.util;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.constant.RedisConstant;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName ApiUserUtils
 * @Deacription TODO
 * @Author cmm api用户类
 * @Date 2021/3/1 15:51
 * @Version 1.0
 **/
@Component
public class ApiUserUtils {
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private static RedisCache redisCacheUtil;
    // 项目启动时把redisCache交给静态的redisCacheUtil
    @PostConstruct
    public void init() {
        redisCacheUtil = this.redisCache;
    }

        /**
         * 获取用户真实名字
         **/
    public static String getUserRelname()
    {
        try
        {
            return getBusinesTUserr().getNickName();
        }
        catch (Exception e)
        {
            throw new CustomException("获取用户账户异常", HttpStatus.UNAUTHORIZED);
        }
    }
    /**
     * 获取用户ID
     **/
    public static Long getUserId()
    {
        try
        {
            return getBusinesTUserr().getUserId();
        }
        catch (Exception e)
        {
            throw new CustomException("获取用户账户异常", HttpStatus.UNAUTHORIZED);
        }
    }
    /**
     * 获取昵称
     **/
    public static String getNickName()
    {
        try
        {
            return getBusinesTUserr().getNickName();
        }
        catch (Exception e)
        {
            throw new CustomException("获取用户账户异常", HttpStatus.UNAUTHORIZED);
        }
    }
    /**
     * 获取头像
     **/
    public static String getAvatarUrl()
    {
        try
        {
            return getBusinesTUserr().getAvatar();
        }
        catch (Exception e)
        {
            throw new CustomException("获取用户账户异常", HttpStatus.UNAUTHORIZED);
        }
    }
/*    *//**
     * 获取用户推荐码
     **//*
    public static String getReferralCode()
    {
        try
        {
            return getBusinesTUserr().getReferralCode();
        }
        catch (Exception e)
        {
            throw new CustomException("获取用户账户异常", HttpStatus.UNAUTHORIZED);
        }
    }*/
    /**
     * 获取用户
     **/
    public static SysUser getBusinesTUserr()
    {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token=request.getHeader("token");
        SysUser businesTUser= redisCacheUtil.getCacheObject(RedisConstant.API_LOGIN_USER_TOKEN+"_"+token);
        if (businesTUser==null){
            throw new CustomException("获取用户账户异常", HttpStatus.UNAUTHORIZED);
        }else{
            return  businesTUser;
        }


    }
    /**
     * 获取用户token
     *
     **/
    public static String  getToken()
    {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token=request.getHeader("token");
        SysUser businesTUser= redisCacheUtil.getCacheObject(RedisConstant.API_LOGIN_USER_TOKEN+"_"+token);
        if (businesTUser==null){
            throw new CustomException("获取用户账户异常", HttpStatus.UNAUTHORIZED);
        }else{
            return  token;
        }


    }
    /**
     * 获取用户电话
     **/
    public static String getUserPhone()
    {
        try
        {
            return getBusinesTUserr().getUserName();
        }
        catch (Exception e)
        {
            throw new CustomException("获取用户账户异常", HttpStatus.UNAUTHORIZED);
        }
    }
}
